﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

namespace Stone.Data
{
    public class DataProviderManagerFactory
    {
        //private static DataProviderManagerFactory dataProviderManager;
        //static DataProviderManagerFactory()
        //{
        //    dataProviderManager = new DataProviderManagerFactory();
        //}

        //public static DataProviderManagerFactory Current
        //{
        //    get { return dataProviderManager; }
        //}

        //public static IDataProviderManager CurrentDefaultManager
        //{
        //    get { return dataProviderManager.Default; }
        //    set { dataProviderManager.Default = value; }
        //}

        private object syncRoot = new object();
        private Dictionary<string, IDataProviderManager> managers;

        public DataProviderManagerFactory()
        {
            managers = new Dictionary<string, IDataProviderManager>(100);
            ResearchManagers();
        }

        public IDataProviderManager this[string key]
        {
            get
            {
                if (String.IsNullOrEmpty(key)) return null;
                lock(syncRoot)
                {
                    key = key.Trim().ToUpper();
                    if (managers.ContainsKey(key))
                    {
                        return managers[key];
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }

        public IDataProviderManager[] GetAllManagers()
        {
            IDataProviderManager[] returnManagers = new IDataProviderManager[managers.Count];
            managers.Values.CopyTo(returnManagers, 0);
            return returnManagers;
        }

        private IDataProviderManager defaultManager;
        public IDataProviderManager Default
        {
            get
            {
                lock (syncRoot) { return defaultManager; }
            }
            set
            {
                lock (syncRoot) { defaultManager = value; }
            }
        }

        public void ResearchManagers()
        {
            ResearchManagers(System.AppDomain.CurrentDomain.BaseDirectory);
        }

        public void ResearchManagers(string searchPath)
        {
            lock (syncRoot)
            {
                managers.Clear();
                if (Directory.Exists(searchPath))
                {
                    string[] files = Directory.GetFiles(searchPath, "Stone.Data.*.dll", SearchOption.AllDirectories);

                    for (int i = 0; i < files.Length; i++)
                    {
                        IDataProviderManager manager = Dare.Utilities.ReflectionUtil.GetClassFrom<IDataProviderManager>(files[i], Path.GetFileNameWithoutExtension(files[i]) + ".DataProviderManager");
                        if (manager != null)
                        {
                            managers[manager.Key.Trim().ToUpper()] = manager;

                            if (defaultManager == null)
                            {
                                defaultManager = manager;
                            }
                        }
                    }
                }
            }
        }
    }
}
